<!DOCTYPE html>
<html xmlns:v-model="http://www.w3.org/1999/xhtml">
<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>传智健康</title>
    <meta name="description" content="传智健康">
    <meta name="keywords" content="传智健康">
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">


    <style type="text/css">
        body, html{width: 100%;height: 100%;margin:0;font-family:"微软雅黑";font-size:14px;}
        #l-map{height:300px;width:100%;}
        #r-result{width:100%;}
    </style>

    <script type="text/javascript" src="//api.map.baidu.com/api?v=2.0&ak=fV2DN6q7eEOadUeSEVGYu2xkfDj4lKk9"></script>



    <!-- 引入样式 -->
    <link rel="stylesheet" href="../plugins/elementui/index.css">
    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../css/style.css">
    <!-- 引入组件库 -->
    <script type="text/javascript" src="../js/jquery.min.js"></script>
    <script src="../js/vue.js"></script>
    <script src="../plugins/elementui/index.js"></script>
    <script src="../js/axios-0.18.0.js"></script>


    <style>
        .datatable {
            position: relative;
            box-sizing: border-box;
            -webkit-box-flex: 1;
            width: 100%;
            max-width: 100%;
            font-size: 14px;
            color: rgb(96, 98, 102);
            overflow: hidden;
            flex: 1 1 0%;
        }
        .datatable td, .datatable th {
            padding: 12px 0;
            min-width: 0;
            -webkit-box-sizing: border-box;
            box-sizing: border-box;
            text-overflow: ellipsis;
            vertical-align: middle;
            position: relative;
            text-align: left;
        }


        .tangram-suggestion
        {
            z-index:9999;}

    </style>

</head>
<body class="hold-transition">
<div id="app">
    <div class="content-header">
        <h1>首页<small>公司分布图</small></h1>
        <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>公司分布图</el-breadcrumb-item>
        </el-breadcrumb>

    </div>
    <div class="app-container">
        <div style="height: 30px">
            <a href="address.html" style="font-size: small;">分布图</a> &nbsp;&nbsp;
            <a href="" style="font-size: small;" target="right">详细信息</a>
        </div>
        <div class="box">
            <div class="filter-container">
                <el-input placeholder="公司地址" v-model="pagination.queryString" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter"></el-input>
                <el-button @click="findPageByPageString()" class="dalfBut">查询</el-button>
                <el-button type="primary" class="butT" @click="handleCreate()">新建</el-button>
            </div>

            <el-table size="small" current-row-key="id" :data="dataList" stripe highlight-current-row>
                <el-table-column type="index" align="center" label="序号"></el-table-column>
                <el-table-column prop="address" label="公司地址" align="center"></el-table-column>
                <el-table-column prop="lng" label="精度" align="center"></el-table-column>
                <el-table-column prop="lat" label=纬度 align="center"></el-table-column>
                <el-table-column label="操作" align="center">
                    <template slot-scope="scope">
                        <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>

            <div class="pagination-container">
                <el-pagination
                        class="pagiantion"
                        @current-change="handleCurrentChange"
                        :current-page="pagination.currentPage"
                        :page-size="pagination.pageSize"
                        layout="total, prev, pager, next, jumper"
                        :total="pagination.total">
                </el-pagination>
            </div>

            <div class="add-form" >
                <el-dialog title="新增地址" :visible.sync="dialogFormVisible">
                    <div id="r-result">
                        <input type="text" id="suggestId" style="width:300px;height: 25px" />
                        <el-button type="primary" size="small" @click="handleAddress()">确认添加</el-button></div><br>
                    <div id="searchResultPanel" style="border:1px solid #C0C0C0;width:150px;height:auto; display:none;"></div>
                    <div id="l-map"></div>
                </el-dialog>

            </div>
        </div>
    </div>
</div>
</body>

<script>

    var vue = new Vue({
        el: '#app',
        data:{
            showSettingCon:false,
            dialogVisible: false,
            activeName:'first',//添加/编辑窗口Tab标签名称
            pagination: {//分页相关属性
                currentPage: 1,
                pageSize:10,
                total:100,
                queryString:null,
            },
            dataList: [], // 列表数据
            dialogFormVisible: false,//控制添加窗口显示/隐藏
            data:{ // 选择地点信息
                address:'',
                lng:'',
                lat:'',
            }
        },
        created() {
            this.findPage();
        },
        methods: {

            //添加
            handleAddress () {
                this.data.address = $("#suggestId").val();
                var lng = this.data.lng;
                if (lng == null || lng.length == 0){
                    this.$message.error("请在地图上选择地址");
                    return false;
                }

               // console.log(this.data);
                axios.post("/address/add.do",this.data).then((res) =>{
                    if (res.data.flag){
                        this.$message({
                            message: res.data.message,
                            type: 'success'
                        });
                        this.pagination.currentPage = "1";
                        this.findPage();
                    } else {
                        this.$message.error(res.data.message);
                    }
                    // 隐藏添加窗口
                    this.dialogFormVisible = false;
                })

            },
            //分页查询
            findPage() {
                var param = {
                    currentPage: this.pagination.currentPage,
                    pageSize: this.pagination.pageSize,
                    queryString: this.pagination.queryString
                };

                axios.post("/address/findPage.do",param).then((res) => {
                    this.dataList = res.data.rows;
                    this.pagination.total = res.data.total;
                })
            },
            findPageByPageString(){
                this.pagination.currentPage = "1";
                this.findPage();
            },

            // 弹出添加窗口
            handleCreate() {
               this.dialogFormVisible = true;
                this.$nextTick(() => {
                    // 渲染chart
                    this.fun1();
                    //console.log(this.data)

                })
            },

            //切换页码
            handleCurrentChange(currentPage) {
                this.pagination.currentPage = currentPage;
                this.findPage();
            },
            // 删除
            handleDelete(row) {
                this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    axios.get("/address/delete.do?id="+row.id).then((res) => {
                        if (res.data.flag){ // 删除成功
                            this.dataList = res.data.data;
                            this.$message({
                                message: res.data.message,
                                type: 'success'
                            });
                            this.findPage()
                        } else { // 删除失败
                            this.$message.error(res.data.message);
                        }
                    })

                }).catch(() => {
                    this.$message({
                        message: "操作已取消",
                        type: 'info'
                    });
                });
            },

            // 加载地图
            fun1() {
                // 百度地图API功能
                function G(id) {
                    return document.getElementById(id);
                }

                var map = new BMap.Map("l-map");
                map.centerAndZoom("北京",12);  // 初始化地图,设置城市和地图级别。
                map.enableScrollWheelZoom(true);  //添加滚轮缩放

                var geolocation = new BMap.Geolocation();
                geolocation.getCurrentPosition(function(r){
                    if(this.getStatus() == BMAP_STATUS_SUCCESS){
                        var mk = new BMap.Marker(r.point);
                        map.addOverlay(mk);
                        map.panTo(r.point);
                        //alert('您的位置：'+r.point.lng+','+r.point.lat);
                    }
                    else {
                        alert('failed'+this.getStatus());
                    }
                });



                var ac = new BMap.Autocomplete(    //建立一个自动完成的对象
                    {"input" : "suggestId"
                        ,"location" : map
                    });

                ac.addEventListener("onhighlight", function(e) {  //鼠标放在下拉列表上的事件
                    var str = "";
                    var _value = e.fromitem.value;
                    var value = "";
                    if (e.fromitem.index > -1) {
                        value = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
                    }
                    str = "FromItem<br />index = " + e.fromitem.index + "<br />value = " + value;

                    value = "";
                    if (e.toitem.index > -1) {
                        _value = e.toitem.value;
                        value = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
                    }
                    str += "<br />ToItem<br />index = " + e.toitem.index + "<br />value = " + value;
                    G("searchResultPanel").innerHTML = str;
                });


                var myValue;
                ac.addEventListener("onconfirm", function(e) {    //鼠标点击下拉列表后的事件
                    var _value = e.item.value;
                    myValue = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
                    G("searchResultPanel").innerHTML ="onconfirm<br />index = " + e.item.index + "<br />myValue = " + myValue;

                    setPlace();
                });



                function setPlace(){
                    map.clearOverlays();    //清除地图上所有覆盖物
                    function myFun(){
                        var pp = local.getResults().getPoi(0).point;    //获取第一个智能搜索的结果
                        map.centerAndZoom(pp, 18);
                        map.addOverlay(new BMap.Marker(pp));    //添加标注
                    }
                    var local = new BMap.LocalSearch(map, { //智能搜索
                        onSearchComplete: myFun
                    });
                    local.search(myValue);
                }

                // 百度地图API功能
                var geoc = new BMap.Geocoder();


                var _this = this.data;
                map.addEventListener("click", function(e){
                    // 清理之前标注点
                    var len =map.getOverlays().length
                    for (var i = len ;i>0; i--) {
                        map.removeOverlay(map.getOverlays()[i]);
                    }
                    var pt = e.point;
                    var marker = new BMap.Marker(pt);
                    // 点击添加标注点
                    map.addOverlay(marker);


                    // 获取选中经纬度
                    _this.lng = pt.lng;
                    _this.lat = pt.lat;
                    geoc.getLocation(pt, function(rs){
                        var addComp = rs.addressComponents;
                       // alert(addComp.province + ", " + addComp.city + ", " +pt.lng+","+pt.lat+ ","+ addComp.district + ", " + addComp.street + ", " + addComp.streetNumber);
                    });
                });

            }

            }
    });

</script>




</html>
